<?php
namespace Admin\Controller;
use Think\Controller;
use Common\BaseController;
use Think\Upload;
use Common\Common\Log;
class NodeController extends BaseController {


    //权限列表
    public function node_list(){
        $templData['title']="权限列表";
        $Node = M('Node'); // 实例化User对象
        $count      = $Node->where('status=1')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $Node->where('status=1')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($list as $k=>$v){
            if($v['pid']!=0){
                $list[$k]['node']=$v['controller']."@".$v['action'];
            }else{
                $list[$k]['node']=$v['controller'];
            }
        }
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign($templData);
        $this->display();
    }

    /**
     * 权限修改
     */
    public function edit(){
        $tmplData['title'] = '权限修改'; // 标题

        if(I( 'post.' )){
            if(session('checkDataKey')==$this->createCheckDataKey(I( 'post.' ))){
                // 验证已通过
                $data['node_name'] = I( 'post.node_name' );
                $data['controller'] = I( 'post.controller' );
                $data['action'] = I( 'post.act' );
                $data['pid'] = I( 'post.pid' );
                $data['content'] = I( 'post.content' );
                $nodeId=I('post.nodeId');
                $url='node_list';
                if(M( 'Node' )->data($data)->where('id="%d"',$nodeId)->save()){
                    $content='权限修改成功';
                    Log::write2($content);
                    $this->showMsg('修改成功！','success',$url);
                }else{
                    $this->showMsg('修改失败！','error',$url);
                }
            }else{
                $this->showMsg('操作异常！','warning','node_list');
            }
        }else{
            //查询记录
            $nodeId=I('get.nodeId');
            $re=M('Node')->where('id="%d"',$nodeId)->find();
            if($re){
                $tmplData['nodeId']=$re['id'];
                $tmplData['node_name']=$re['node_name'];
                $tmplData['controller']=$re['controller'];
                $tmplData['act']=$re['action'];
                $tmplData['content']=$re['content'];
                $tmplData['pid']=$re['pid'];
            }else{
                $url='node_list';
                $this->showMsg('操作异常！','warning',$url);
            }
            $node=M("Node")->where("status=1 AND pid=0")->select();
            $tmplData['node']=$node;
            $this->assign( $tmplData );
            $this->display('Node_node_edit');
        }
    }
    //删除角色
    public function del()
    {
        $id=I("get.nodeId");
        $url='node_list';
        if(M("Node")->where('id="%d"',$id)->setField('status',0))
        {
            $content='权限删除成功';
            Log::write2($content);
            $this->showMsg('删除成功！','success',$url);
        }else{
            $this->showMsg('删除失败！','error',$url);
        }
    }

    //验证权限名称唯一性
    public function checkNodeUnique()
    {
        if(I('post.node_name')){
                if(!I("post.id")){
                    $where='node_name="%s"';
                }else{
                    $id=I("post.id");
                    $where='node_name="%s" AND id!='.$id.'';
                }
            if(M('Node')->where($where,I('post.node_name'))->count()){
                $data=array(
                    'tips'=>'该角色已经存在！',
                );
            }else{
                $data=array(
                    'tips'=>'',
                );
            }
        }else{
            $data=array(
                'tips'=>'系统异常！',
            );
        }
        $this->ajaxReturn($data);
    }

   //添加权限
    public function node_add()
    {
        $tmplData['title'] = '新增权限'; // 标题
        if(I( 'post.' )){
            if(session('checkDataKey')==$this->createCheckDataKey(I( 'post.' ))){
                // 验证已通过
                $data['node_name'] = I( 'post.node_name' );
                $data['controller'] = I( 'post.controller');
                $data['action'] = I( 'post.act');
                $data['content'] = I( 'post.content');
                $data['pid'] = I( 'post.pid');
                if(M( 'Node' )->data( $data )->add()){
                    $lastInsId=M('Node')->getLastInsID();
                    $url='node_list';
                    $param='nodeId='.$lastInsId;
                    $content='新增权限成功';
                    Log::write2($content);
                    $this->showMsg('添加成功！','success',$url,$param);
                }else{
                    $this->showMsg('添加失败！','error',"node_list");
                }
            }else{
                $this->showMsg('操作异常！','warning');
            }
        }
        $node=M("node")->where("status=1 AND pid=0")->select();
        $this->assign("node",$node);
        $this->assign( $tmplData );
        $this->display();
    }
    //权限分配
    public function node_set()
    {
        $this->display();
    }

    /**
     * 数据验证
     */
    public function ajaxCheckData(){
        // 验证场景
        $scene = I( 'get.scene' );
        // 验证数据
        $data = I( 'post.' );
        switch($scene){
            case 'add' :
                $Role = D( 'Node' );
                // error:0:没有错误,1:有错误 errInfo:有错时为错误信息(数组格式),否则为空, verify:验证通过置为ok,否则置为空
                if(!$Role->create( $data ,'add' )){
                    // 验证不通过
                    $reData = array(
                        'error' => 1,
                        'errInfo' => $Role->getError(),
                    );
                }else{
                    // 验证通过
                    session('checkDataKey',$this->createCheckDataKey($data));
                    $reData = array(
                        'error' => 0,
                        'errInfo' => '',
                    );
                }
                break;
            case 'edit' :
                $Node = D( 'Node' );
                $Node->id=$data['id'];
                // error:0:没有错误,1:有错误 errInfo:有错时为错误信息(数组格式),否则为空, verify:验证通过置为ok,否则置为空
                if(!$Node->create( $data ,'edit' )){
                    // 验证不通过
                    $reData = array(
                        'error' => 1,
                        'errInfo' => $Node->getError(),
                    );
                }else{
                    // 验证通过
                    session('checkDataKey',$this->createCheckDataKey($data));
                    $reData = array(
                        'error' => 0,
                        'errInfo' => '',
                    );
                }
                break;
            default	:
                $reData = array(
                    'error' => 1,
                    'errInfo' => '',
                );
                break;
        }
        $this->ajaxReturn( $reData );
    }

}